#import <Foundation/Foundation.h>
#import "TTTableNeedFullItem.h"
#import "TTTableNeedFullItemCell.h"
#import "TTTableHelpItem.h"
#import "TTTableHelpItemCell.h"
#import "INDefaultStylesheet.h"
#import "NeedHelpsViewDataSource.h"
#import "NeedHelpsModel.h"
#import "Need.h"
#import "Help.h"

#define POST_ROW_HEIGHT         120
#define kSectionHeaderHight     25.0

@implementation NeedHelpsViewDataSource

///////////////////////////////////////////////////////////////////////////////////////////////////
// public

- (void)tableViewDidLoadModel:(UITableView *)tableView
{
	
    [super tableViewDidLoadModel:tableView];
	[tableView setEditing:NO animated:YES];
	
    [self.items removeAllObjects];
	
    // Construct an object that is suitable for the table view system
    // from each SearchResult domain object that we retrieve from the TTModel.
    for (Need *result in [(id<NeedHelpsModel>)self.model results]) {
		
        [self.items addObject:[TTTableNeedFullItem itemWithText:result.text 
												displayname:[NSString stringWithFormat:@"@%@",result.user.nickname] 
													   fullname:[NSString stringWithFormat:@"%@ %@", result.user.name, result.user.lastname]
													subject:result.subject 
												  timestamp:[NSDate dateWithTimeIntervalSinceNow:[result.timediff doubleValue]]
												 pictureurl:result.user.pictureurl
													  city:result.city
														URL:nil]];
		
	    for (Help *help in result.allHelps) {
		NSString *userUrl = [NSString stringWithFormat:@"in://userView/?user=%@", help.user._id];
//		[self.items addObject: [TTTableMessageItem itemWithTitle:help.user.displayname
//														 caption:help.text
//															text:help.text
//													   timestamp:[NSDate dateWithTimeIntervalSinceNow:[help.timediff doubleValue]]
//														imageURL:help.user.pictureurl
//															 URL:userUrl]];
//			
//			
			
        [self.items addObject:[TTTableHelpItem itemWithText:[TTStyledText textFromXHTML:help.text] 
												displayname:help.user.displayname 
												  timestamp:[NSDate dateWithTimeIntervalSinceNow:[help.timediff doubleValue]]
												 pictureurl:help.user.pictureurl
														URL:userUrl]];
		//TTLOG(@"Added %@ need ", help._id);
		}
		
	
	}
	
	

	
	//******	
	
	//tableView.tableHeaderView = view;
	
//    NSLog(@"Added %lu search result objects", (unsigned long)[self.items count]);
}

///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc {
	[super dealloc];
}


- (Class)tableView:(UITableView*)tableView cellClassForObject:(id) object { 
	
//	if ([object isKindOfClass:[TTTableNeedItem class]]) { 
//		return [TTTableNeedItemCell class]; 		
//	} else { 
//		return [super tableView:tableView cellClassForObject:object]; 
//	}
//	
	if ([object isKindOfClass:[TTTableHelpItem class]]) { 
		return [TTTableHelpItemCell class]; 		
	} 
	else if ([object isKindOfClass:[TTTableNeedFullItem class]]) { 
		return [TTTableNeedFullItemCell class]; 		
	}
	else
	{ 
		return [super tableView:tableView cellClassForObject:object]; 
	}
}

- (void)tableView:(UITableView*)tableView prepareCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//	cell.selectedBackgroundView.backgroundColor = TTSTYLEVAR(cellBackgroundColor);

//	cell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//	cell.selectedBackgroundView.backgroundColor = [UIColor yellowColor];

}

@end
